package signfactory

import (
	"fmt"
	"strings"
)

var signHandlers map[string]ISignHandler

func init() {
	signHandlers = make(map[string]ISignHandler)
	register("hmac", newHMACSignHandler())
	register("md5", newMD5SignHandler())
	register("rsa", newRSASignHandler())
	register("sha1", newSHA1SignHandler())
	register("sha256", newSHA256SignHandler())
	register("sha512", newSHA512SignHandler())
}

//Get 获取处理对象
func Get(name string) (ISignHandler, error) {
	name = strings.ToLower(name)
	handler, ok := signHandlers[name]
	if !ok {
		return nil, fmt.Errorf("签名类型未配置:%s", name)
	}
	return handler, nil
}

func register(name string, handler ISignHandler) {
	if handler == nil || name == "" {
		return
	}
	signHandlers[name] = handler
}
